package com.ruoyi.wtww.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wtww.domain.WtwwKxjl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.wtww.domain.WtwwFj;
import com.ruoyi.wtww.mapper.WtwwMapper;
import com.ruoyi.wtww.domain.Wtww;
import com.ruoyi.wtww.service.IWtwwService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 委托委外Service业务层处理
 *
 * @author huacai
 * @date 2024-08-12
 */
@Service
public class WtwwServiceImpl implements IWtwwService
{
    @Resource
    private WtwwMapper wtwwMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询委托委外
     *
     * @param wtwwId 委托委外主键
     * @return 委托委外
     */
    @Override
    public Wtww selectWtwwByWtwwId(String wtwwId)
    {
        return wtwwMapper.selectWtwwByWtwwId(wtwwId);
    }

    /**
     * 查询委托委外列表
     *
     * @param wtww 委托委外
     * @return 委托委外
     */
    @Override
    public List<Wtww> selectWtwwList(Wtww wtww)
    {
        return wtwwMapper.selectWtwwList(wtww);
    }

    /**
     * 新增委托委外
     *
     * @param wtww 委托委外
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWtww(Wtww wtww)
    {
        wtww.setCreateTime(DateUtils.getNowDate());
        wtww.setIdWhere("存在");
        //插入用户昵称到创建人
        wtww.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        int rows = wtwwMapper.insertWtww(wtww);
        insertWtwwFj(wtww);
        insertWtwwKxjl(wtww);
        return rows;
    }

    /**
     * 批量新增委托委外
     *
     * @param wtwws 委托委外List
     * @return 结果
     */
    @Override
    public int batchInsertWtww(List<Wtww> wtwws)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(wtwws)) {
            try {
                for (int i = 0; i < wtwws.size(); i++) {
                    int row = wtwwMapper.insertWtww(wtwws.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == wtwws.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改委托委外
     *
     * @param wtww 委托委外
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWtww(Wtww wtww)
    {
        wtww.setUpdateTime(DateUtils.getNowDate());
//        wtwwMapper.deleteWtwwFjByWtwwId(wtww.getWtwwId());
        wtwwMapper.deleteWtwwKxjlByWtwwId(wtww.getWtwwId());
        insertWtwwFj(wtww);
        insertWtwwKxjl(wtww);
        return wtwwMapper.updateWtww(wtww);
    }

    /**
     * 批量删除委托委外
     *
     * @param wtwwIds 需要删除的委托委外主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWtwwByWtwwIds(String[] wtwwIds)
    {
        wtwwMapper.deleteWtwwFjByWtwwIds(wtwwIds);
        wtwwMapper.deleteWtwwKxjlByWtwwIds(wtwwIds);
        return wtwwMapper.deleteWtwwByWtwwIds(wtwwIds);
    }

    /**
     * 删除委托委外信息
     *
     * @param wtwwId 委托委外主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWtwwByWtwwId(String wtwwId)
    {
        wtwwMapper.deleteWtwwFjByWtwwId(wtwwId);
        wtwwMapper.deleteWtwwKxjlByWtwwId(wtwwId);
        return wtwwMapper.deleteWtwwByWtwwId(wtwwId);
    }

    /**
     * 新增附件
     * @param wtwwFj 委托委外
     * @return
     */
    @Override
    public int insertWtwwFjByWtwwId(WtwwFj wtwwFj) {
        return wtwwMapper.insertWtwwFjByWtwwId(wtwwFj);
    }

    /**
     * 根据ID查询附件列表
     * @param wtwwFj
     * @return
     */
    @Override
    public List<WtwwFj> selectWtwwFjList(WtwwFj wtwwFj) {
        return wtwwMapper.selectWtwwFjList(wtwwFj);
    }

    /**
     * 批量删除委托委外附件资料
     *
     * @param fjIds 需要删除的委托委外附件资料主键
     * @return 结果
     */
    @Override
    public int deleteWtwwFjByFjIds(Integer[] fjIds)
    {
        return wtwwMapper.deleteWtwwFjByFjIds(fjIds);
    }

    /**
     * 锁定委托委外
     * @param wtww
     * @return
     */
    @Override
    public int updateSdztLocking(Wtww wtww) {
        return wtwwMapper.updateSdztLocking(wtww);
    }

    /**
     * 解锁委托委外
     * @param wtww
     * @return
     */
    @Override
    public int updateSdztUnlock(Wtww wtww) {
        return wtwwMapper.updateSdztUnlock(wtww);
    }

    /**
     * 获取委托委外与款项记录详细信息
     * @param wtwwId
     * @return
     */
    @Override
    public Wtww selectWtwwKxJlByWtwwId(String wtwwId) {
        return wtwwMapper.selectWtwwKxJlByWtwwId(wtwwId);
    }

    /**
     * 新增委托委外附件资料信息
     *
     * @param wtww 委托委外对象
     */
    public void insertWtwwFj(Wtww wtww)
    {
        List<WtwwFj> wtwwFjList = wtww.getWtwwFjList();
        String wtwwId = wtww.getWtwwId();
        if (StringUtils.isNotNull(wtwwFjList))
        {
            List<WtwwFj> list = new ArrayList<WtwwFj>();
            for (WtwwFj wtwwFj : wtwwFjList)
            {
                wtwwFj.setWtwwId(wtwwId);
                list.add(wtwwFj);
            }
//            if (list.size() > 0) {
//                wtwwMapper.batchWtwwFj(list);
//            }
        }
    }

    /**
     * 新增款项记录信息
     *
     * @param wtww 委托委外对象
     */
    public void insertWtwwKxjl(Wtww wtww)
    {
        List<WtwwKxjl> wtwwKxjlList = wtww.getWtwwKxjlList();
        String wtwwId = wtww.getWtwwId();
        if (StringUtils.isNotNull(wtwwKxjlList))
        {
            List<WtwwKxjl> list = new ArrayList<WtwwKxjl>();
            for (WtwwKxjl wtwwKxjl : wtwwKxjlList)
            {
                wtwwKxjl.setWtwwId(wtwwId);
                list.add(wtwwKxjl);
            }
            if (list.size() > 0)
            {
                wtwwMapper.batchWtwwKxjl(list);
            }
        }
    }
}
